package lobster.net.verificationcode;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class SMSBroadcastReceiver extends BroadcastReceiver {
    private String TAG = "SMSBroadcastReceiver";
    private static MessageListener mMessageListener;

    public SMSBroadcastReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] pdus = (Object[]) intent.getExtras().get("pdus");
        for (Object pdu : pdus) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
            String sender = smsMessage.getDisplayOriginatingAddress();
            String content = smsMessage.getMessageBody();
            // long date = smsMessage.getTimestampMillis();
            // Date timeDate = new Date(date);
            // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // String time = simpleDateFormat.format(timeDate);
            //
            Log.i(TAG, "短信来自: " + sender);
            Log.i(TAG, "短信内容: " + content);



            mMessageListener.OnReceived(content);


            // 如果短信来自1069009558666,不再往下传递,一般此号码可以作为短信平台的号码。
//            if (BaseParams.SMS_SENDER.equals(sender)) {
            // Logger.i(TAG, "已经转发");
//                abortBroadcast();
//            }
        }
    }

    /**
     * @author TinhoXu E-mail:xth@erongdu.com
     * @version 创建时间：2015年9月2日 下午5:37:47
     *          回调接口
     */
    public interface MessageListener {
        public void OnReceived(String message);
    }

    public void setOnReceivedMessageListener(MessageListener messageListener) {
        this.mMessageListener = messageListener;
    }


}